package com.zhangxiaofanfan.hotcode.second;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * TODO
 *
 * @author zhangxiaofanfan
 * @date 2024-05-29 21:58:26
 */
public class HotCode037 {
    public static void main(String[] args) {

    }

    public int maxDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        Deque<TreeNode> deque = new ArrayDeque<>();
        deque.add(root);
        int layer = 0, curLayerCount = 0;
        while (!deque.isEmpty()) {
            layer++;
            curLayerCount = deque.size();
            for (int i = 0; i < curLayerCount; i++) {
                TreeNode first = deque.removeFirst();
                if (first.left != null) deque.addLast(first.left);
                if (first.right != null) deque.addLast(first.right);
            }
        }
        return layer;
    }

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
